package br.com.emmis.tercons.receiver;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import java.util.Calendar;

public class BootReceiver extends BroadcastReceiver {

    private static final String DEBUG_TAG = "BootReceiver";

    public BootReceiver() {
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d(DEBUG_TAG, "onReceive - start");
        if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
            Calendar updateTime = Calendar.getInstance();
            updateTime.setTimeInMillis(System.currentTimeMillis());
            updateTime.set(Calendar.HOUR_OF_DAY, 9);
            updateTime.set(Calendar.MINUTE, 00);

            Intent downloader = new Intent(context, AlarmReceiver.class);
            PendingIntent recurringDownload = PendingIntent.getBroadcast(context,
                    0, downloader, PendingIntent.FLAG_CANCEL_CURRENT);

            AlarmManager alarms = (AlarmManager) context.getSystemService(
                    Context.ALARM_SERVICE);

            alarms.setInexactRepeating(AlarmManager.RTC_WAKEUP,
                    updateTime.getTimeInMillis(),
                    AlarmManager.INTERVAL_FIFTEEN_MINUTES, recurringDownload);
        }
    }
}
